package com.fu.edu.manager.dao;

import com.fu.edu.manager.domain.Student;

/**
 * @author 付大大
 * @create 2021-01-06 10:53
 */
public class StudentDao implements BaseStudentDao {
    private static Student[] students = new Student[5];

    static {
        Student student1 = new Student("2006910242", "付大大", "23", "20170101");
        Student student2 = new Student("2006910241", "周大大", "23", "20170101");
        students[0] = student1;
        students[1] = student2;
    }

    public boolean addStudent(Student student) {

        int index = -1;

        for (int i = 0; i < students.length; i++) {
            if (students[i] == null) {
                index = i;
                break;
            }
        }


        if (index == -1) {
            //装满了
            return false;
        } else {
            //没有装满，正常添加，返回true
            students[index] = student;
            return true;
        }
    }

    public Student[] findAllStudent() {
        return students;
    }

    public void deleteStudentById(String id) {
        int index = getIndex(id);
        students[index] = null;
    }

    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student != null && student.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateStudent(Student student, String id) {
        int index = getIndex(id);
        students[index] = student;
    }
}
